package edu.hgnc.aop.xml.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;

import java.util.Arrays;

// @Aspect
// @Component
// @Order(200)
public class LogUtil {

	private static final Log logger = LogFactory.getLog(LogUtil.class);

	// @Pointcut("execution(public Integer edu.hgnc.aop.service.MyCalculator.*(Integer,Integer)")
	public void myPointCut(){}

	// @Pointcut("execution(* *()")
	public void myPointCut1(){}

	// @Before(value = "myPointCut()")
	private int start(JoinPoint joinPoint){
	//	获取方法签名
		Signature signature = joinPoint.getSignature();
	//	获取参数信息
		Object[] args = joinPoint.getArgs();

		logger.debug("log---"+signature.getName()+"方法开始执行:参数是"+ Arrays.asList(args));
		return 100;
	}

	// @AfterReturning(value = "myPointCut()",returning = "result")
	public static  void stop(JoinPoint joinPoint,Object result){
		Signature signature = joinPoint.getSignature();
		logger.debug("log---"+signature.getName()+"方法执行结束，执行结果是"+ result);
	}

	// @AfterThrowing(value = "myPointCut()",throwing = "e")
	public static void logException(JoinPoint joinPoint,Exception e){
		Signature signature = joinPoint.getSignature();
		logger.debug("log---"+signature.getName()+"方法抛出异常，异常是"+ e);
	}

	// @After(value = "myPointCut()")
	public static void logException(JoinPoint joinPoint){
		Signature signature = joinPoint.getSignature();
		logger.debug("log---"+signature.getName()+"方法执行结束");
	}

	// @After(value = "myPointCut()")
	public static void logFinally(JoinPoint joinPoint){
		Signature signature = joinPoint.getSignature();
		logger.debug("log---"+signature.getName()+"方法执行结束");
	}

	// @Around("myPointCut()")
	public Object around(ProceedingJoinPoint pjp) throws Throwable{
		Signature signature = pjp.getSignature();
		Object[] args = pjp.getArgs();
		Object result = null;
		try {
			logger.debug("log---环绕通知start"+ signature.getName()+"方法开始执行，参数为："+Arrays.asList(args));
			result  = pjp.proceed();
		//	result = 100;
			logger.debug("log---环绕通知stop"+ signature.getName()+"方法执行结束");

		}catch (Throwable throwable){
			logger.debug("log---环绕异常通知"+ signature.getName()+"方法出现异常");
			throw throwable;
		}finally {
			logger.debug("log---环绕返回通知"+ signature.getName()+"方法返回结果是："+result);
		}
		return result;
	}
}
